package com.app.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;



/**
 * @author ice
 */
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.app.config","com.app.service"})
@MapperScan({"com.app.mapper"})
public class ApplicationConfig {

    @Autowired
    PropertiesConfig propertiesConfig;

    /**
     * 设置数据源
     * @return
     */
    @Bean
    DataSource dataSource() {

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(propertiesConfig.getDriver());
        druidDataSource.setUrl(propertiesConfig.getUrl());
        druidDataSource.setUsername(propertiesConfig.getUserName());
        druidDataSource.setPassword(propertiesConfig.getPassWord());
        return druidDataSource;
    }


    /**
     * 配置MyBatis  SqlSessionFactoryBean
     * @return
     */
    @Bean
    SqlSessionFactoryBean sqlSessionFactoryBean(){
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        return  sqlSessionFactoryBean;
    }
    /**
     * 事务管理
     * @return
     */
    @Bean
    PlatformTransactionManager transactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource());
        return dataSourceTransactionManager;
    }




}
